package com.codingvip.server;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.codingvip.server.impl.UserServiceImpl;

import java.io.IOException;

import com.alibaba.dubbo.common.URL;
import java.util.List;

public class SimpleServer {
    public void openService(int port) {
        //服务配置
        ServiceConfig serviceConfig = new ServiceConfig();
        //设置服务接口
        serviceConfig.setInterface(UserService.class);
        //开放协议
        serviceConfig.setProtocol(new ProtocolConfig("dubbo", port));
        //设置一个空的注册中心
        serviceConfig.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
//        serviceConfig.setRegistry(new RegistryConfig("zookeeper://192.168.0.147:2181"));
        //设置服务当前所在应用
        serviceConfig.setApplication(new ApplicationConfig("simple-app"));
        //接口实现
        UserServiceImpl ref=new UserServiceImpl();
        serviceConfig.setRef(ref);



        //暴露服务
        serviceConfig.export();

        List<URL> list = serviceConfig.getExportedUrls();
        if (list != null && list.size() > 0) {
            int ppp =list.get(0).getPort();
            ref.setPort(ppp);
            System.out.print("服务已启动" + ppp);
        }
    }

    public static void main(String[] args) throws IOException {
        //-1 时从20880 开始，每启动一次 ；端口+1
        new SimpleServer().openService(-1);
        System.in.read();
    }
}
